/**   
 * @Title: StringUtil.java 
 * @Package com.esun.util 
 * @Description: 字符串转换工具类 
 * @author Miko baoz@500wan.com   
 * @date 2011-5-23 下午03:14:00  
 * copy right 深圳市易讯天空网络技术有限公司
 */
package com.esun.util;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import com.esun.cheetah.config.Key;
import com.esun.cheetah.config.Language;
import com.esun.util.log.EDebug;

import android.net.ParseException;

/**
 * @ClassName: StringUtil
 * @Description: 字符串转换工具类
 * @author Miko baoz@500wan.com
 * @date 2011-5-23 下午03:14:00
 * 
 */
public class StringUtil {

	/**
	 * 转换一个二进制数组为字符串
	 * 
	 * @param bytes
	 *            需要转换的二进制数组
	 * @return 转换后的字符串
	 */
	public static String bytesToString(byte[] bytes) {
		return bytesToString(bytes, 0, bytes.length);
	}

	/**
	 * 转换一个二进制数组中指定长度数据为字符串
	 * 
	 * @param bytes
	 *            需要转换的二进制数组
	 * @param off
	 *            开始转换的起始位置
	 * @param len
	 *            转换的长度
	 * @return 转换后的字符串
	 */
	public static String bytesToString(byte[] bytes, int off, int len) {
		// System.out.println(System.getProperty("microedition.encoding"));
		String ret = null;
		if (bytes != null) {
			try {
				ret = new String(bytes, off, len, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				ret = new String(bytes, off, len);// +e.getMessage();
				e.printStackTrace();
			}
		}
		return ret;
	}

	// 日期比较
	public static long getDifferValue(String dateBig, String dateSmall) throws ParseException {

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		/**
		 * 两天相差的豪秒数
		 */
		long dif = 0;
		try {
			dif = df.parse(dateBig).getTime() - df.parse(dateSmall).getTime();
		} catch (java.text.ParseException e) {
			e.printStackTrace();
		}
		/**
		 * 最后把豪秒换算成天 要怎么舍 怎么入 自己决定
		 */
		long dayDiff = dif / (long) (24 * 3600000);
		return dayDiff;

	}

	public static int getStringLengthChar(String str) {
		int length = 0;
		String str_temp = null;

		try {
			str_temp = new String(str.getBytes("GBK"), "ISO8859_1");
			length = str_temp.length();
		} catch (UnsupportedEncodingException e) {
			EDebug.o("StringUtil", "编码转换异常" + e.toString());
		}

		return length;
	}

	public static boolean isContain(String[] strList, String strItem) {

		boolean isContain = false;
		int size = strList.length;

		for (int i = 0; i < size; i++) {
			if (strList[i].equals(strItem)) {
				isContain = true;
				break;
			}
		}

		return isContain;

	}
	
	/**
	 * @Title: comJosn
	 * @Description: json串组装
	 * @param HashMap
	 *            <String, String> 参数hashmap
	 * @return String 组装后的json串
	 * @throws
	 */
	public static String comJson(HashMap<String, String> hmJson) {
		
		  boolean isNull = false ;
		
			// 组装后的json串
			String json = Language.SPEC_LEFT_BRACES;
			// 键集合
			Set<String> mapSet = hmJson.keySet();
			Iterator<String> itor = mapSet.iterator();
			while (itor.hasNext()) {
				// 组装json
				String key = itor.next();
				if (!key.equals(Key.KEY_ID)) {
					json += Language.SPEC_QUOTATION_MARKS + key
							+ Language.SPEC_QUOTATION_MARKS_COLON
							+ Language.SPEC_QUOTATION_MARKS + hmJson.get(key)
							+ Language.SPEC_QUOTATION_MARKS_COMMA;
					
					isNull = true ;
				}

			}
			
			if (isNull) {
				json = json.substring(0, json.length() - 1);
			} else {
				json = json.substring(0, json.length());
			}
			
			json += Language.SPEC_RIGHT_BRACES;

			return json;
	}

}
